﻿using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UIWindows.GlobalStatics;
using XCode;
using System.IO;
using UIWindows.Models.Devices;
using UIWindows.Tools.Helper;
using UIWindows.Views;
using XingLucifer.Devices;
using XingLucifer.IBase.Enums;
using XingLucifer.IBase;
using NPOI.SS.Formula.Functions;
using Kinlo.MESDocking;

namespace UIWindows.ViewModels
{
    public class ElectricityStatementViewModel
    {
        private IMESInteraction _interaction;
        public ElectricityMetersDataModel EntityModel { get; set; }
        public Axis[] XAxes { get; set; }
        public ObservableCollection<ObservableValue> _electricityList { get; set; }
        public ISeries[] Series { get; set; }

        // Creates a gray background and border in the draw margin.
        public DrawMarginFrame DrawMarginFrame => new()
        {
            Fill = new SolidColorPaint(new SKColor(220, 220, 220)),
            Stroke = new SolidColorPaint(new SKColor(180, 180, 180), 1)
        };
        private string _path;
        private DeviceConfigStatic _deviceConfigStatic;
        private ParameterStatic _parameterStatic;
        public ElectricityStatementViewModel(DeviceConfigStatic deviceConfigStatic, ParameterStatic parameterStatic)
        {
            _interaction = new WastageDataUpload(DialogLogHelper.MesLog, null);
            _deviceConfigStatic = deviceConfigStatic;
            _parameterStatic = parameterStatic;
            _electricityList = new ObservableCollection<ObservableValue>(new ObservableValue[12]
            {
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0),
                new ObservableValue(0)
            });
            Series = new ISeries[] {
                new LineSeries<ObservableValue>
                {
                    Values = _electricityList,
                    Fill = new SolidColorPaint(SKColors.CornflowerBlue), // mark
                    Stroke = null,
                    GeometryFill = null,
                    GeometryStroke = null
                },
            };
            XAxes = new Axis[]
            {
                new Axis
                {
                    Labels = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" },
                    LabelsPaint = new SolidColorPaint
                    {
                        Color = SKColors.Black,
                    }
                },
            };
            _path = Path.Combine(GlobalStatic.ConfigPath, "EStatement.txt");
            if (File.Exists(_path))
            {
                string str_value = File.ReadAllText(_path, Encoding.UTF8);
                if (!string.IsNullOrEmpty(str_value))
                {
                    try
                    {
                        EntityModel = System.Text.Json.JsonSerializer.Deserialize<ElectricityMetersDataModel>(str_value, GlobalStatic.Options);
                    }
                    catch (Exception ex)
                    {
                        $"电能表 读取历史数据异常：{ex}".MessageDialogs(XingLucifer.IBase.Enums.MessageLevelType.错误, hostDialog:Enums.PopUpTypes.Global);
                    }
                }
            }
            if (EntityModel == null)
            {
                EntityModel = new ElectricityMetersDataModel()
                {
                    PowerConsumption_Chart = new List<double>(new double[12]),
                    PowerConsumption_Rated = 0
                };
            }
            for (int i = 0; i < EntityModel.PowerConsumption_Chart.Count; i++)
            {
                _electricityList[i].Value = EntityModel.PowerConsumption_Chart[i];
            }
            Task.Run(() => { 
                Thread.Sleep(6000);
                //Loop();
            });
        }

  

        private bool TimeInterval(DateTime dateTime) => DateTime.Now.Subtract(dateTime).Hours >= 1;
    }
}
